package com.en.sharing.serviceprovider.bpm;

import com.en.sharing.serviceprovider.bpm.approval.ApprovalService;

/**
 * 审批测试
 *
 * @author 付晓飞
 * @date 2024/3/11 7:44 下午
 */
public class ApprovalTest {

    /**
     * 模拟 spring bean
     */
    public final static ApprovalService approvalService = new ApprovalService();
    public final static OperationApprovalProcess<Person, Boolean> process = new OperationApprovalProcess<>(approvalService);

    static {
        // 处理方法注册 - 提交代码
        process.register(OperationEntryCodeEnum.PERSON, OperateTypeEnum.UPDATE, ApprovalTest::update);
    }

    public static void main(String[] args) {
        // 构建审批任务
        final Person person = new Person("小明", "男");
        final Person nPerson = new Person("小明", "女");
        // 提交任务 - 提交代码
        process.submit(OperateApprovalTaskBo.newTaskForUpdate(OperationEntryCodeEnum.PERSON, person, nPerson));
        // 原来的代码
        // update(person, nPerson);
        // 通知
        approvalService.approval(true, process);
    }


    public static boolean update(Person oldData, Person newData) {
        System.out.println(oldData.getName() + " 修改为： " + newData.getName());
        System.out.println(oldData.getSex() + " 修改为： " + newData.getSex());
        return true;
    }

    public static class Person {
        private String name;
        private String sex;

        public Person(String name, String sex) {
            this.name = name;
            this.sex = sex;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getSex() {
            return sex;
        }

        public void setSex(String sex) {
            this.sex = sex;
        }
    }

    /**
     * 操作枚举定义 - 提交代码
     */
    public enum OperationEntryCodeEnum implements IOperationEntryCode {
        PERSON("person", true);

        private String code;
        private boolean openApproval;

        OperationEntryCodeEnum(String code, boolean openApproval) {
            this.code = code;
            this.openApproval = openApproval;
        }

        @Override
        public String getCode() {
            return code;
        }

        @Override
        public boolean openApproval() {
            return openApproval;
        }
    }

}
